package com.feihong.txgw.utils;

import android.app.Activity;
import android.content.Intent;

import com.alibaba.fastjson.JSON;
import com.feihong.txgw.json.recharge.SubmitOrderInfo;
import com.feihong.txgw.ui.activity.order.PayActivity;
import com.tsy.sdk.pay.weixin.WXPay;

/**
 * 2017.7.4
 * 张鹏
 * 充值页解析结果工具类
 */
public class RechargePayUtils {
    public static void paseJson(final Activity activity, String result, int type) {

        SubmitOrderInfo orderInfo = JSON.parseObject(result, SubmitOrderInfo.class);
        if (orderInfo == null) {
            ToastUtils.getInstance().showShortToast("未获取到数据");
            return;
        }
        String order_id = orderInfo.getOrder_sn(); // 订单号
        String price = orderInfo.getPrice(); // 价格
        String order_name = orderInfo.getOrder_name(); // 商品名称
        String time = orderInfo.getTime(); //

        Intent intent = new Intent(activity, PayActivity.class);
        intent.putExtra(PayActivity.INTENT_ORDER_ID, order_id); // 订单号
        intent.putExtra(PayActivity.INTENT_PRICE, price); // 价格
        intent.putExtra(PayActivity.INTENT_PAY_TYPE, type);
        activity.startActivity(intent);
    }

    /**
     * 微信支付
     *
     * @param pay_param 支付服务生成的支付参数
     */
    public static void doWXPay(String pay_param) {

        WXPay.getInstance().doPay(pay_param, new WXPay.WXPayResultCallBack() {
            @Override
            public void onSuccess() {
                ToastUtils.getInstance().showShortToast("支付成功");
            }

            @Override
            public void onError(int error_code) {
                switch (error_code) {
                    case WXPay.NO_OR_LOW_WX:
                        ToastUtils.getInstance().showShortToast("未安装微信或微信版本过低");
                        break;

                    case WXPay.ERROR_PAY_PARAM:
                        ToastUtils.getInstance().showShortToast("参数错误");
                        break;

                    case WXPay.ERROR_PAY:
                        ToastUtils.getInstance().showShortToast("支付失败");
                        break;
                    default:break;
                }
            }

            @Override
            public void onCancel() {
                ToastUtils.getInstance().showShortToast("支付取消");
            }
        });
    }

}
